package event

import (
	"fmt"
	"github.com/ian-kent/go-log/log"
)

type EventParams struct {
	Param []interface{}
}

func NewEventParams(param ...interface{}) *EventParams {
	return &EventParams{
		Param: param,
	}
}

// EventWatchEventCb 事件回调函数
type EventWatchEventCb = func(eventId int, param *EventParams)

var eventMap = make(map[int]EventWatchEventCb)

func DispatchEvent(eventId int, param *EventParams) error {
	cb, ok := eventMap[eventId]
	if !ok {
		return nil
	}
	cb(eventId, param)
	return nil
}

func RegeditEvent(eventId int, cb EventWatchEventCb) error {
	if _, ok := eventMap[eventId]; ok {
		log.Error("重复的事件注册 %v", eventId)
		return fmt.Errorf("重复的事件注册")
	}
	eventMap[eventId] = cb
	return nil
}

var idCreator int

// NewId  id 生成器
func NewId() int {
	idCreator++
	return idCreator
}
